<?php

namespace Modules\Chinadmin\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Modules\System\Models\ConfigModel;
use Modules\System\Services\Mds\MDSArray;
use Modules\Chinadmin\Models\FormModel;

class System extends Base
{

    /**
     *
     */
    public function __construct(){

    }

    /**
     * http://cheyoukong.1zhetejia.com/chinadmin/admin/System/systemUpgrade
     * 可以考虑获取动态配置来取不同的参数
     *
     */
    public function systemUpgrade(){

        $data = [];

        $data['Modules'] = 'chinadmin';
        $data['class'] = 'System';
        $data['i_i'] = "{}";
        $data['id'] = 1;

        $data['group'] = ["ios","Android"];

        $Config = ConfigModel::whereIn("group",$data['group'])->get();
        $data['this_data'] = MDSArray::arraykeyVal($Config,"field","val");

        $data['form'] = FormModel::whereIn("table_name",["ca_ios","ca_Android"])
            ->get();

        $data['group'] = json_encode($data['group']);
        return view('chinadmin::admin.System.systemSet',$data);

    }

    /**
     * http://cheyoukong.1zhetejia.com/chinadmin/admin/System/systemSet
     * 可以考虑获取动态配置来取不同的参数
     *
     */
    public function systemSet(){

        $data = [];

        $data['Modules'] = 'zuoban';
        $data['class'] = 'System';
        $data['i_i'] = "{}";
        $data['id'] = 1;

        $data['group'] = ["ios","alipay","tengxun","application","tengxunIM"];

        $Config = ConfigModel::whereIn("group",$data['group'])->get();
        $data['this_data'] = MDSArray::arraykeyVal($Config,"field","val");

        $data['form'] = FormModel::where("isHide",0)
            ->whereIn("table_name",["ca_ios","ca_alipay","ca_tengxun","ca_application","ca_tengxunIM"])
            ->orderBy("table_name","asc")
            ->get();

        $data['group'] = json_encode($data['group']);

        //d($data['form']);

        return view('chinadmin::admin.System.systemSet',$data);

    }

    public function create(Request $request){

        $i = $request->input();
        unset($i[1]);
        unset($i['default']);
        unset($i['s']);

        $group = MDSArray::jsonToArray($i['group']);
        unset($i['group']);
        //d($i['group']);

        $Config = ConfigModel::whereIn("group",$group)->get();

        $data['this_data'] = MDSArray::arraykeyVal($Config,"field","val");

        foreach($data['this_data'] as $k=>$v){
            if($i[$k] != $v){
                ConfigModel::where("field",$k)->update([
                    "val"=>$i[$k]
                ]);
            }
        }

        mdsResponse(200,"更新成功");

    }



}
